/**********************************************************************************************************
** 驱动文件：	ups-FR_UK3XXX.c
** 驱动类型：	ups
** 设备名称：	UPS监测
** 设备型号：	FR_UK3XXX
** 设备品牌：	科华
** 设备协议：	RS232C 的智能三相UPS通信协议
** 驱动描述：	适用于科华FR-UK系列三进三出和三进单出UPS状态监测，协议类型为RS232C通信协议，指令为G1<cr>、G2<cr>、G3<cr>
** 生成日期：	2018-05-19 22:11:38
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var BatteryVoltage;							//电池电压
	Var BatteryCapacity;						//电池容量
	Var BatteryRemainingTime;					//电池剩余供电时间
	Var BatteryCurrent;							//电池电流
	Var Temperature;							//UPS温度
	Var InputFrequency;							//输入频率
	Var BypassFrequency;						//旁路频率
	Var OutputFrequency;						//输出频率
	Var InputPhaseSequence;						//输入相序
	Var BatteryUnderVoltageProtect;				//电池欠压保护
	Var BatteryLowVoltage;						//电池欠压
	Var InputOutputType;						//输入输出类型
	Var WorkMode;								//运行模式
	Var BatteryChargingState;					//电池充电状态
	Var RectifierState;							//整流器状态
	Var BypassFrequencyState;					//旁路频率状态
	Var BypassAirSwitchState;					//旁路空开状态
	Var BypassState;							//旁路状态
	Var StaticSwitchState;						//静态开关状态
	Var InverterState;							//逆变器状态
	Var EmergencyShutdown;						//紧急关机
	Var BusbarVervoltageShutdown;				//母线过压关机
	Var BypassAirSwitchClosed;					//旁路空开闭合关机
	Var OverloadShutdown;						//过载关机
	Var OutputVoltageAbnormalShutdown;			//输出电压异常关机
	Var OverTemperatureShutdown;				//过温关机
	Var OutputShortCircuitShutdown;				//输出短路关机
	Var InputVoltageA;							//A相输入电压
	Var InputVoltageB;							//B相输入电压
	Var InputVoltageC;							//C相输入电压
	Var BypassVoltageA;							//A相旁路电压
	Var BypassVoltageB;							//B相旁路电压
	Var BypassVoltageC;							//C相旁路电压
	Var OutputVoltageA;							//A相输出电压
	Var OutputVoltageB;							//B相输出电压
	Var OutputVoltageC;							//C相输出电压
	Var LoadRateA;								//A相负载率
	Var LoadRateB;								//B相负载率
	Var LoadRateC;								//C相负载率
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		INTEGER,
		0,
		READONLY,
		"BatteryVoltage",
		"电池电压",
		"V",
		"电池电压",
		0,
		0,
		"电池电压过高",
		"电池电压过低",
		0,
		0
	},
	{
		INTEGER,
		0,
		READONLY,
		"BatteryCapacity",
		"电池容量",
		"%",
		"电池容量",
		0,
		0,
		"电池容量充足",
		"电池容量不足",
		0,
		0
	},
	{
		INTEGER,
		0,
		READONLY,
		"BatteryRemainingTime",
		"电池剩余供电时间",
		"分钟",
		"电池剩余供电时间",
		0,
		0,
		"电池备份时间不足",
		"电池备份时间充足",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryCurrent",
		"电池电流",
		"A",
		"电池充/放电电流",
		0,
		0,
		"电池电流过高",
		"电池电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Temperature",
		"UPS温度",
		"℃",
		"UPS温度",
		0,
		0,
		"UPS温度过高",
		"UPS温度过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"InputFrequency",
		"输入频率",
		"Hz",
		"输入频率",
		0,
		0,
		"输入频率过高",
		"输入频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassFrequency",
		"旁路频率",
		"Hz",
		"旁路频率",
		0,
		0,
		"旁路频率过高",
		"旁路频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputFrequency",
		"输出频率",
		"Hz",
		"输出频率",
		0,
		0,
		"输出频率过高",
		"输出频率过低",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InputPhaseSequence",
		"输入相序",
		"",
		"输入相序：正常、接反",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryUnderVoltageProtect",
		"电池欠压保护",
		"",
		"电池欠压保护：无、欠压保护",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryLowVoltage",
		"电池欠压",
		"",
		"电池欠压：无，欠压",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InputOutputType",
		"输入输出类型",
		"",
		"输入输出类型：三进三出、三进单出",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"WorkMode",
		"运行模式",
		"",
		"运行方式：0-电池充电，市电正常，在线运行，1-电池放电，后备式运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryChargingState",
		"电池充电状态",
		"",
		"电池充电状态：浮充、均充",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"RectifierState",
		"整流器状态",
		"",
		"整流器运行：正常运行、故障",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BypassFrequencyState",
		"旁路频率状态",
		"",
		"旁路频率状态：正常、异常",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BypassAirSwitchState",
		"旁路空开状态",
		"",
		"手动旁路状态：断开、闭合",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BypassState",
		"旁路状态",
		"",
		"旁路交流电状态：正常、故障",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"StaticSwitchState",
		"静态开关状态",
		"",
		"静态开关状态：旁路模式、逆变模式",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterState",
		"逆变器状态",
		"",
		"逆变器运行：正常运行、故障",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"EmergencyShutdown",
		"紧急关机",
		"",
		"紧急关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BusbarVervoltageShutdown",
		"母线过压关机",
		"",
		"母线过压关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BypassAirSwitchClosed",
		"旁路空开闭合关机",
		"",
		"旁路空开闭合关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OverloadShutdown",
		"过载关机",
		"",
		"过载关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OutputVoltageAbnormalShutdown",
		"输出电压异常关机",
		"",
		"输出电压异常关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OverTemperatureShutdown",
		"过温关机",
		"",
		"过温关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OutputShortCircuitShutdown",
		"输出短路关机",
		"",
		"输出短路关机：无、发生",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"InputVoltageA",
		"A相输入电压",
		"V",
		"A相输入电压",
		0,
		0,
		"A相输入电压过高",
		"A相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"InputVoltageB",
		"B相输入电压",
		"V",
		"B相输入电压",
		0,
		0,
		"B相输入电压过高",
		"B相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"InputVoltageC",
		"C相输入电压",
		"V",
		"C相输入电压",
		0,
		0,
		"C相输入电压过高",
		"C相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassVoltageA",
		"A相旁路电压",
		"V",
		"A相旁路电压",
		0,
		0,
		"A相旁路电压过高",
		"A相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassVoltageB",
		"B相旁路电压",
		"V",
		"B相旁路电压",
		0,
		0,
		"B相旁路电压过高",
		"B相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassVoltageC",
		"C相旁路电压",
		"V",
		"C相旁路电压",
		0,
		0,
		"C相旁路电压过高",
		"C相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputVoltageA",
		"A相输出电压",
		"V",
		"A相输出电压",
		0,
		0,
		"A相输出电压过高",
		"A相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputVoltageB",
		"B相输出电压",
		"V",
		"B相输出电压",
		0,
		0,
		"B相输出电压过高",
		"B相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputVoltageC",
		"C相输出电压",
		"V",
		"C相输出电压",
		0,
		0,
		"C相输出电压过高",
		"C相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"LoadRateA",
		"A相负载率",
		"%",
		"A相输出负载率",
		0,
		0,
		"A相输出负载率过高",
		"A相输出负载率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"LoadRateB",
		"B相负载率",
		"%",
		"B相输出负载率",
		0,
		0,
		"B相输出负载率过高",
		"B相输出负载率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"LoadRateC",
		"C相负载率",
		"%",
		"C相输出负载率",
		0,
		0,
		"C相输出负载率过高",
		"C相输出负载率过低",
		0,
		0
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			*len = sprintf(cmd,"G1\r");
			break;
		case 1:
			*len = sprintf(cmd,"G2\r");
			break;	
		case 2:
			*len = sprintf(cmd,"G3\r");
			break;
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	Data_Struct 	*d = (Data_Struct*)data;
	buf[len] = '\0';
	if(buf!=NULL && buf[0]=='!' && len>0 && buf[len-1]=='\r')
	{	
		switch(index)
		{
			case 0:
				sscanf(buf,"!%d %d %d %f %f %f %f %f\r",&d->BatteryVoltage.value.inum,&d->BatteryCapacity.value.inum,&d->BatteryRemainingTime.value.inum,&d->BatteryCurrent.value.fnum,&d->Temperature.value.fnum,&d->InputFrequency.value.fnum,&d->BypassFrequency.value.fnum,&d->OutputFrequency.value.fnum);					
				break;
			case 1:
			//整流器和直流输入状态
				d->InputPhaseSequence.value.str 				= buf[2]=='1'?"接反":"正常";			//输入相序
				Set_Var_Status(&d->InputPhaseSequence, 			buf[2]=='1'?Alarm:Normal, NULL);	
				d->BatteryUnderVoltageProtect.value.str			= buf[3]=='1'?"欠压保护":"无";			//电池欠压保护				
				Set_Var_Status(&d->BatteryUnderVoltageProtect,	buf[3]=='1'?Alarm:Normal, NULL);	
				d->BatteryLowVoltage.value.str 					= buf[4]=='1'?"欠压":"无";				//电池欠压
				Set_Var_Status(&d->BatteryLowVoltage,			buf[4]=='1'?Alarm:Normal, NULL);					
				d->InputOutputType.value.str 					= buf[5]=='1'?"三进单出":"三进三出";	//输入输出类型
				d->WorkMode.value.str 							= buf[6]=='1'?"后备式运行":"在线式运行";//供电模式
				Set_Var_Status(&d->WorkMode, 					buf[6]=='1'?Alarm:Normal, NULL);
				d->BatteryChargingState.value.str 				= buf[7]=='1'?"均充":"浮充";			//电池充电状态
				d->RectifierState.value.str						= buf[8]=='1'?"正常运行":"故障";		//整流器运行状态
				Set_Var_Status(&d->RectifierState, 				buf[8]=='1'?Normal:Alarm, NULL);		
			//UPS工作状态
				d->BypassFrequencyState.value.str				= buf[13]=='1'?"异常":"正常";			//整流器异常
				Set_Var_Status(&d->BypassFrequencyState, 		buf[13]=='1'?Alarm:Normal, NULL);
				d->BypassAirSwitchState.value.str				= buf[14]=='1'?"闭合":"断开";			//手动旁路状态
				Set_Var_Status(&d->BypassAirSwitchState, 		buf[14]=='1'?Alarm:Normal, NULL);		
				d->BypassState.value.str						= buf[15]=='1'?"正常":"异常";			//旁路交流电状态
				Set_Var_Status(&d->BypassState, 				buf[15]=='1'?Normal:Alarm, NULL);
				d->StaticSwitchState.value.str					= buf[16]=='1'?"逆变模式":"旁路模式";	//静态开关状态
				Set_Var_Status(&d->StaticSwitchState, 			buf[16]=='1'?Normal:Alarm, NULL);
				d->InverterState.value.str						= buf[17]=='1'?"正常运行":"故障";		//逆变器运行状态
				Set_Var_Status(&d->InverterState, 				buf[17]=='1'?Normal:Alarm, NULL);				
			//逆变器故障状态
				d->EmergencyShutdown.value.str					= buf[20]=='1'?"发生":"无";				//紧急停机
				Set_Var_Status(&d->EmergencyShutdown, 			buf[20]=='1'?Alarm:Normal, NULL);			
				d->BusbarVervoltageShutdown.value.str			= buf[21]=='1'?"发生":"无";				//直流输入过高停机
				Set_Var_Status(&d->BusbarVervoltageShutdown, 	buf[21]=='1'?Alarm:Normal, NULL);	
				d->BypassAirSwitchClosed.value.str				= buf[22]=='1'?"发生":"无";				//手动旁路闭合停机
				Set_Var_Status(&d->BypassAirSwitchClosed, 		buf[22]=='1'?Alarm:Normal, NULL);	
				d->OverloadShutdown.value.str					= buf[23]=='1'?"发生":"无";				//过载停机
				Set_Var_Status(&d->OverloadShutdown, 			buf[23]=='1'?Alarm:Normal, NULL);
				d->OutputVoltageAbnormalShutdown.value.str 		= buf[24]=='1'?"发生":"无";				//逆变器输出电压异常停机
				Set_Var_Status(&d->OutputVoltageAbnormalShutdown,buf[24]=='1'?Alarm:Normal,NULL);
				d->OverTemperatureShutdown.value.str			= buf[25]=='1'?"发生":"无";				//过温停机
				Set_Var_Status(&d->OverTemperatureShutdown, 	buf[25]=='1'?Alarm:Normal, NULL);
				d->OutputShortCircuitShutdown.value.str			= buf[26]=='1'?"发生":"无";				//输出短路停机
				Set_Var_Status(&d->OutputShortCircuitShutdown, 	buf[26]=='1'?Alarm:Normal, NULL);
				break;	
			case 2://三相电实时信息参数
				sscanf(buf,"!%f/%f/%f %f/%f/%f %f/%f/%f %f/%f/%f\r",&d->InputVoltageA.value.fnum,&d->InputVoltageB.value.fnum,&d->InputVoltageC.value.fnum,&d->BypassVoltageA.value.fnum,&d->BypassVoltageB.value.fnum,&d->BypassVoltageC.value.fnum,&d->OutputVoltageA.value.fnum,&d->OutputVoltageB.value.fnum,&d->OutputVoltageC.value.fnum,&d->LoadRateA.value.fnum,&d->LoadRateB.value.fnum,&d->LoadRateC.value.fnum);
				break;
				
			default:
				break;
		}
		return 0;
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void ups_FR_UK3XXX_Registration(void)
* 函数说明： UPS监测驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void ups_FR_UK3XXX_Registration(void)
{
	DeviceDriverRegistration(
		"ups",													//设备类型
		"UPS监测",												//设备名称（导航栏默认显示名称）
		"科华",													//设备品牌
		"FR_UK3XXX",											//设备型号
		"适用于科华FR-UK系列三进三出UPS状态监测，协议类型为RS232C通信协议，指令为G1<cr>、G2<cr>、G3<cr>",//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		800
	);
		
#if 0
	#define	CMD1	"G1\r"
	#define	RSP1	"!395 100 0000 000.0 +35.0 49.9 49.9 49.9\r"
	#define	CMD2	"G2\r"
	#define	RSP2	"!00000001 00001111 00000000\r"
	#define	CMD3	"G3\r"
	#define	RSP3	"!235.0/234.0/238.0 236.0/233.0/237.0 220.0/220.0/220.0 000.0/000.0/000.0\r"					
	VirtualFrameRegistration(CMD1, sizeof(CMD1)-1, RSP1, sizeof(RSP1)-1);
	VirtualFrameRegistration(CMD2, sizeof(CMD2)-1, RSP2, sizeof(RSP2)-1);
	VirtualFrameRegistration(CMD3, sizeof(CMD3)-1, RSP3, sizeof(RSP3)-1);						
#endif	
}

